package peishui.network.http;

import android.os.Message;

import com.alibaba.fastjson.JSON;
import com.android.volley.NetworkResponse;
import com.android.volley.Request;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.HttpHeaderParser;
import com.android.volley.toolbox.StringRequest;
import com.xkh.xkh.R;

import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.xkh.xkh.MyApplication;
import managers.UIHandler;
import managers.VolleySingleton;
import shuiyuqing.network.beans.RealTimeIrrBRWaterDam;
import shuiyuqing.network.http.DownloadInfo;

/**
 * 获取渠道的所有闸门的最新信息
 * Created by 凯 on 2015/11/10.
 */
public class GetRealCannelDam {

    Upload upload;
    Download download;

    public Download getDownload() {
        return download;
    }

    public GetRealCannelDam(Upload upload) {
        this.upload = upload;
    }

    // 获取
    public void get() {
        String IP = MyApplication.share().getResources().getString(R.string.IP);
        String url = "http://" + IP + "/guanqu/PeiShui";
        StringRequest stringRequest = new StringRequest(Request.Method.POST, url,
                new Response.Listener<String>() {
                    @Override
                    public void onResponse(String response) {
                        afterGet(response);
                    }
                }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {

            }
        }) {
            @Override
            protected Map<String, String> getParams() {
                Map map = new HashMap();
//                map.put("data", stadiaCode);
                map.put("data", JSON.toJSONString(upload));
                return map;
            }

            @Override
            protected Response<String> parseNetworkResponse(NetworkResponse response) {
                String temp = null;
                try {
                    temp = new String(response.data, "utf-8");
                } catch (UnsupportedEncodingException e) {
                    e.printStackTrace();
                }
                return Response.success(temp, HttpHeaderParser.parseCacheHeaders(response));
            }
        };
        VolleySingleton.getVolleySingleton(MyApplication.share()).addToRequestQueue(stringRequest);
    }

    // 收到回复
    private void afterGet(String response) {
        download = JSON.parseObject(response, Download.class);
        if (download.getStatus() == DownloadInfo.SUCCESS) {

            // 更新listview
            Message message = Message.obtain();
            message.what = 0x54;
            message.obj = GetRealCannelDam.this;
            UIHandler.uiHandler.dispatchMessage(message);

        } else if (download.getStatus() == DownloadInfo.FAILED) {

        }
    }


    // 上行数据

    public static class Upload {
        ArrayList<String> stadiaCodes = new ArrayList();

        public Upload(ArrayList<String> stadiaCodes) {
            this.stadiaCodes = stadiaCodes;
        }

        public ArrayList<String> getStadiaCodes() {
            return stadiaCodes;
        }

        public void setStadiaCodes(ArrayList<String> stadiaCodes) {
            this.stadiaCodes = stadiaCodes;
        }


    }

    // 下行数据
    public static class Download {
        //		ArrayList<GateWithDam> gateWithDams = new ArrayList<>();
//
//		public ArrayList<GateWithDam> getGateWithDams() {
//			return gateWithDams;
//		}
//
//		public void setGateWithDams(ArrayList<GateWithDam> gateWithDams) {
//			this.gateWithDams = gateWithDams;
//		}
        int status;

        ArrayList<GateWithDam> result = new ArrayList<GateWithDam>();


        public int getStatus() {
            return status;
        }

        public void setStatus(int status) {
            this.status = status;
        }

        public ArrayList<GateWithDam> getResult() {
            return result;
        }

        public void setResult(ArrayList<GateWithDam> result) {
            this.result = result;
        }

        // 闸门和其最新信息
        public static class GateWithDam {
            String gateCode;// 闸门编码
            String gateName; // 闸门名称

            List<RealTimeIrrBRWaterDam> waterDams = new ArrayList<RealTimeIrrBRWaterDam>();

            public String getGateCode() {
                return gateCode;
            }

            public void setGateCode(String gateCode) {
                this.gateCode = gateCode;
            }

            public String getGateName() {
                return gateName;
            }

            public void setGateName(String gateName) {
                this.gateName = gateName;
            }

            public List<RealTimeIrrBRWaterDam> getWaterDams() {
                return waterDams;
            }

            public void setWaterDams(List<RealTimeIrrBRWaterDam> waterDams) {
                this.waterDams = waterDams;
            }

        }
    }
}
